package com.chandra.pos.dao.impl;

import com.chandra.pos.dao.RoleDAO;
import com.chandra.pos.dao.entity.RoleEntity;
import com.chandra.pos.dao.entity.UserRoleEntity;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * UserEntity: chkumar
 * Date: 6/1/12
 * Time: 10:47 AM
 * To change this template use File | Settings | File Templates.
 */
@Repository

public class RoleDAOImpl implements RoleDAO {

    @Autowired
    private SessionFactory sessionFactory;


    @Override
    public RoleEntity getRole(long roleId) {
        RoleEntity entity = (RoleEntity) sessionFactory.getCurrentSession().get(RoleEntity.class, roleId);
        return entity;
    }

    @Override
    public RoleEntity saveRole(RoleEntity roleEntity) {
        // this.getHibernateTemplate().save(userEntity);
        Long id = (Long) sessionFactory.getCurrentSession().save(roleEntity);

        roleEntity.setId(id);
        return roleEntity;  //
    }

    @Override
    public RoleEntity updateRole(RoleEntity roleEntity) {
        sessionFactory.getCurrentSession().update(roleEntity);
        return roleEntity;  //T
    }

    @Override
    public List<RoleEntity> searchRoleByName(String name) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<RoleEntity> getRoles(int start, int resultSize) {
        Query query = sessionFactory.getCurrentSession().createQuery("from RoleEntity");
        query.setFirstResult(start);
        query.setMaxResults(resultSize);
        //List obj = .createQuery(UserEntity.class);
        return query.list();
    }

    @Override
    public UserRoleEntity addUserRole(UserRoleEntity userRoleEntity) {
        sessionFactory.getCurrentSession().save(userRoleEntity);

        return userRoleEntity;
    }

    @Override
    public boolean removeUserRole(UserRoleEntity userRoleEntity) {
        sessionFactory.getCurrentSession().delete(userRoleEntity);

        return true;
    }
}
